﻿using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace breakoutsharp.visual.gameobject
{
    public class Paddle : IScreenObject
    {
        public const int DefaultHeight = 10;
        public const int DefaultWidth = 50;
        protected PointF location;
        protected SizeF size;
        protected bool visible = true;
        protected bool alive = true;
        protected Color color;

        protected static CustomVertex.PositionTextured[] paddleVertices;
        protected static VertexBuffer paddleBuffer;
		protected static Texture paddleOrangeTexture;
		protected static Texture paddleGreenTexture;

        private void CreatePaddleVertices(Device device)
        {
			/* Following was to be used if textures was wanted instead of plain colouring */
			CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[6];
			verts[0] = new CustomVertex.PositionTextured(new Vector3(0, 0, 0), 0, 0);
			verts[1] = new CustomVertex.PositionTextured(new Vector3(0, Paddle.DefaultHeight * -1, 0), 0, 1);
			verts[2] = new CustomVertex.PositionTextured(new Vector3(Paddle.DefaultWidth, 0, 0), 1, 0);
			verts[3] = new CustomVertex.PositionTextured(new Vector3(0, Paddle.DefaultHeight * -1, 0), 0, 1);
			verts[4] = new CustomVertex.PositionTextured(new Vector3(Paddle.DefaultWidth, Paddle.DefaultHeight * -1, 0), 1, 1);
			verts[5] = new CustomVertex.PositionTextured(new Vector3(Paddle.DefaultWidth, 0, 0), 1, 0);

			Paddle.paddleBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), verts.Length, device, Usage.WriteOnly | Usage.Dynamic, CustomVertex.PositionTextured.Format, Pool.Default);
			Paddle.paddleBuffer.SetData(verts, 0, LockFlags.None);

			/* SAVE VERTICES FOR POTENTIAL LATER USE */
			Paddle.paddleVertices = verts;

			/* Load texture */
			Paddle.paddleGreenTexture = new Texture(device, new Bitmap(@"data\gfx\paddles\paddleGreen.png"), 0, Pool.Managed);
			Paddle.paddleOrangeTexture = new Texture(device, new Bitmap(@"data\gfx\paddles\paddleOrange.png"), 0, Pool.Managed);
        }

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }
        private core.PaddleControl controls;



        public Paddle(core.PaddleControl control, float x, float y, float width, float height)
            : this(control, new PointF(x, y), new SizeF(width, height))
        {

        }
        public Paddle(core.PaddleControl control, float x, float y, float width, float height, Color c)
            : this(control, new PointF(x, y), new SizeF(width, height), c)
        {

        }

        public Paddle(core.PaddleControl control, PointF loc, SizeF size)
            : this(control, loc, size, Color.Black)
        {

        }
        public Paddle(core.PaddleControl control, PointF loc, SizeF size, Color c)
        {
            this.location = loc;
            this.size = size;
            this.controls = control;
            this.color = c;
        }

        #region IScreenObject Members

        public void Draw(Device device)
        {
            if (Paddle.paddleBuffer == null)
            {
                CreatePaddleVertices(device);
            }

            // -- Draw paddles
            device.SetStreamSource(0, Paddle.paddleBuffer, 0);

			if (this.color == Color.Green)
			{
				device.SetTexture(0, Paddle.paddleGreenTexture);

			}
			else
				device.SetTexture(0, Paddle.paddleOrangeTexture);
			device.Transform.World = Matrix.Translation(this.Location.X, -1 * this.Location.Y, 0);
			device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
        }

        public PointF Location
        {
            get
            {
                return location;
            }
            set
            {
                location = value;
            }
        }

        public SizeF Size
        {
            get
            {
                return size;
            }
            set
            {
                size = value;
            }
        }

        public bool Visible
        {
            get
            {
                return visible;
            }
            set
            {
                visible = value;
            }
        }

        public RectangleF GetRectF
        {
            get { return new RectangleF(this.location, this.size); }

        }

        public Rectangle GetRect
        {
            get { return new Rectangle((int)location.X, (int)location.Y, (int)size.Width, (int)size.Height); }
        }

        #endregion

        public bool Alive
        {
            get
            {
                return alive;
            }
            set
            {
                alive = value;
            }
        }

        public core.PaddleControl Controls
        {
            get { return controls; }
            set { controls = value; }
        }


    }
}
